<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/component/pear/css/pear.css}"/>
</head>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container layui-container">
            <div class="layui-row ">
                <div class="layui-col-xs12 layui-col-sm6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="required|username" name="username"
                                   placeholder="请输入" type="text">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">昵称</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="required" name="nickName"
                                   placeholder="请输入" type="text">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="required|pass" name="password"
                                   placeholder="请输入" type="password">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="Phone" name="mobile"
                                   placeholder="请输入" type="text">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="" name="email" placeholder="请输入"
                                   type="text">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="user-save" lay-submit="" type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="pear-btn pear-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/component/layui/layui.js}"></script>
<script th:src="@{/component/pear/pear.js}"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '用户名不能全为数字';
                }

                //如果不想自动弹出默认提示框，可以直接返回 true，这时你可以通过其他任意方式提示（v2.5.7 新增）
                if (value === 'xxx') {
                    alert('用户名不能为敏感词');
                    return true;
                }
            }
            //我们既支持上述函数式的方式，也支持下述数组的形式
            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
        });

        form.on('submit(user-save)', function (data) {
            // let roleIds = "";
            // $('input[type=checkbox]:checked').each(function() {
            //     roleIds += $(this).val()+",";
            // });
            // roleIds = roleIds.substr(0,roleIds.length-1);
            // data.field.roleIds = roleIds;
            console.log(data.field)
            $.ajax({
                url: '/user/save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>